package cn.stylefeng.guns.modular.project.controller;

import cn.hutool.core.date.DateUtil;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.comm.bean.BetweenMonth;
import cn.stylefeng.guns.comm.util.BetweenTimeUtil;
import cn.stylefeng.guns.modular.project.entity.Project;
import cn.stylefeng.guns.modular.project.mapper.ProjectMemberMapper;
import cn.stylefeng.guns.modular.project.model.params.ProjectMemberParam;
import cn.stylefeng.guns.modular.project.model.params.ProjectParam;
import cn.stylefeng.guns.modular.project.model.result.ProjectMemberResult;
import cn.stylefeng.guns.modular.project.service.ProjectMemberService;
import cn.stylefeng.guns.sys.core.shiro.ShiroKit;
import cn.stylefeng.guns.sys.modular.system.entity.Position;
import cn.stylefeng.guns.sys.modular.system.service.PositionService;
import cn.stylefeng.guns.sys.modular.system.service.RoleService;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/member")
public class MemberController extends BaseController {
    @Autowired
    private ProjectMemberService projectMemberService;
    @Autowired
    private ProjectMemberMapper projectMemberMapper;

    private String PREFIX = "/project";

    @RequestMapping("")
    public String index() {
        if (ShiroKit.hasAnyRoles("XMJL,administrator")) {
            return PREFIX + "/member.html";
        } else {
            return detail(ShiroKit.getUser().getId(), "");
        }

    }

    /**
     * 查询列表
     *
     * @author
     * @Date 2019-10-15
     */
    @ResponseBody
    @RequestMapping("/list")
    public LayuiPageInfo list(ProjectMemberParam projectParam) {
        BetweenMonth betweenMonth = BetweenTimeUtil.getBetweenMonth(projectParam.getYearMonth(), "至");
        projectParam.setBetweenMonth(betweenMonth);
        return this.projectMemberService.findAllMember(projectParam);
    }

    @RequestMapping("/detail")
    public String detail(Long userId, String yearMonth) {
        setAttr("userId", userId);
        setAttr("yearMonth", yearMonth);
        ProjectMemberParam projectMemberParam = new ProjectMemberParam();
        projectMemberParam.setUserId(userId);
        BetweenMonth betweenMonth = BetweenTimeUtil.getBetweenMonth(yearMonth, "至");
        projectMemberParam.setBetweenMonth(betweenMonth);
        List<String> years = projectMemberMapper.findYearByUserId(projectMemberParam);
        setAttr("years", years);
        setAttr("thisYear", DateUtil.thisYear());
        return PREFIX + "/member_detail.html";
    }

    @RequestMapping("/workhour")
    @ResponseBody
    public ResponseData userMonthWorkHour(ProjectMemberParam projectMemberParam) {
        BetweenMonth betweenMonth = BetweenTimeUtil.getBetweenMonth(projectMemberParam.getYearMonth(), "至");
        projectMemberParam.setBetweenMonth(betweenMonth);
        List<ProjectMemberResult> workHourByYearUserId = projectMemberMapper.findWorkHourByYearUserId(projectMemberParam);
        return ResponseData.success(workHourByYearUserId);
    }

    @RequestMapping("/projectworkhour")
    @ResponseBody
    public ResponseData projectWorkHour(ProjectMemberParam projectMemberParam) {
        BetweenMonth betweenMonth = BetweenTimeUtil.getBetweenMonth(projectMemberParam.getYearMonth(), "至");
        projectMemberParam.setBetweenMonth(betweenMonth);
        List<ProjectMemberResult> workHourByYearUserId = projectMemberMapper.groupByProjectId(projectMemberParam);
        return ResponseData.success(workHourByYearUserId);
    }
}
